library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.1
library(lavaan)
## Warning: package 'lavaan' was built under R version 4.1.1
library(DiagrammeR)
## Warning: package 'DiagrammeR' was built under R version 4.1.1
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.1
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.1.1

Import data

combined=read.csv("data/monthly_averages/monthly_data_compiled_regions.csv",stringsAsFactors = F)
cnames=read.csv("analysis/column_names_region_monthly.csv", stringsAsFactors = F)
dsub=filter(combined, Year>=1995) %>% arrange(Region,Year,Month)
focaldata=dsub[,cnames$Datacolumn]
fvars=cnames$Shortname
colnames(focaldata)=fvars
regions=unique(focaldata$region)
regionorder=c("Far West","West","North","South")
focaldata=focaldata%>% 
  mutate(decyear=year+(month-1)/12)

focaldata = focaldata %>% 
  mutate(tzoop=hcope+clad+mysid+pcope+rotif_m,
         tzoop_e=hcope_e+clad_e+mysid_e+pcope_e+rotif_e,
         hzoop=hcope+clad+rotif_m,
         hzoop_e=hcope_e+clad_e+rotif_e,
         pzoop=mysid+pcope,
         pzoop_e=mysid_e+pcope_e,
         turbid=-secchi) 
fvars=c(fvars,"tzoop","tzoop_e",
        "hzoop","hzoop_e",
        "pzoop","pzoop_e","turbid")
cnames=rbind(cnames,data.frame(Longname=NA,Shortname=c("tzoop","tzoop_e",
                                                       "hzoop","hzoop_e",
                                                       "pzoop","pzoop_e","turbid"),
                               Diagramname=c("Total Zooplankton\nBiomass",
                                             "Total Zooplankton\nEnergy",
                                             "Herbivorous Zooplankton\nBiomass",
                                             "Herbivorous Zooplankton\nEnergy",
                                             "Predatory Zooplankton\nBiomass",
                                             "Predatory Zooplankton\nEnergy",
                                             "Turbidity"),
                               Datacolumn=NA,Log=c(rep("yes",6),"no"),
                               Color=c("black","black","#ED7D31","#ED7D31","#7030A0",
                                       "#7030A0","#4472C4")))

#focal variables
varnames=c("temp","flow","turbid","dophos","din","chla","hcope","clad","amphi","pcope","mysid","rotif_m","potam","corbic","sside","cent","marfish_bsmt","estfish_bsmt","tzoop","hzoop","pzoop")

#labels for lagged vars
cnameslag=cnames
cnameslag$Shortname=paste0(cnameslag$Shortname,"_1")
cnameslag$Diagramname=paste(cnameslag$Diagramname,"(t-1)")
cnameslag=rbind(cnames,cnameslag)

source("analysis/myLavaanPlot.r")
source("analysis/semDiagramFunctions.r")

Data prep

Log transform, scale.
Within and across regions.
Create set with regional monthly means removed.

#log transform
logvars=fvars[cnames$Log=="yes"]
logtrans=function(x) {
  x2=x[which(!is.na(x))]
  if(any(x2==0)) {log(x+min(x2[which(x2>0)],na.rm=T))}
  else {log(x)}
}
focaldatalog = focaldata %>% 
  mutate_at(logvars,logtrans)

#scale data
fdr0=focaldatalog
tvars=fvars[-(1:3)]

#scaled within regions
fdr=fdr0 %>% 
  group_by(region) %>% 
  #scale
  mutate_at(tvars,scale) %>% 
  #lag
  mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
  ungroup() %>% 
  as.data.frame()

#scaled within regions, remove monthly means
fdr_ds=fdr %>% 
  group_by(region,month) %>%
  mutate_at(tvars,list("mm"=function(x) {mean(x,na.rm = T)})) %>% 
  mutate_at(tvars,function(x) {x-mean(x,na.rm = T)}) %>% 
  ungroup() %>% 
  #lag
  group_by(region) %>% 
  mutate_at(tvars,scale) %>% 
  mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
  ungroup() %>% 
  as.data.frame()

#scaled across regions
# fdr1=fdr0 %>% 
#   #scale
#   mutate_at(tvars,scale) %>% 
#   #lag
#   group_by(region) %>% 
#   mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
#   ungroup() %>% 
#   as.data.frame()

#scaled across regions, monthly means removed
# fdr1_ds=fdr1 %>% 
#   group_by(region,month) %>%
#   mutate_at(tvars,list("mm"=function(x) {mean(x,na.rm = T)})) %>% 
#   mutate_at(tvars,function(x) {x-mean(x,na.rm = T)}) %>% 
#   ungroup() %>% 
#   #lag
#   group_by(region) %>% 
#   mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
#   ungroup() %>% 
#   as.data.frame()

Data availability

Exclude individual zooplankton plankton groups from zooplankton model if rare (95% of values in a region are less than the across site mean, or more than 10% of values in a region are zeros).

sside and cent have no data in FW and W.

marfish and clams excluded if 95% of values in a region are less than the across site mean, though this results in marfish being excluded from W.

FW: exclude clad, mysid, corbic, sside/cent
W: exclude clad, corbic, marfish, sside/cent
N: exclude clad, potam, marfish
S: exclude mysid, potam, marfish

dataavail=focaldata %>% 
  gather(var, value, 4:length(fvars)) %>% 
  group_by(var) %>% 
  mutate(varmean=mean(value, na.rm=T)) %>% ungroup() %>% 
  group_by(region, var) %>% 
  summarize(
    propmissing=length(which(is.na(value)))/length(value),
    propzeros=length(which(value==0))/length(which(!is.na(value))),
    exclude=ifelse(quantile(value,probs = 0.95, na.rm = T)<mean(varmean),T,F)) %>% 
  as.data.frame()
## `summarise()` has grouped output by 'region'. You can override using the `.groups` argument.
#these variables should not be used (too many zeros)
filter(dataavail,propzeros>0.1 | exclude) %>% filter(var %in% c("mysid","hcope","pcope","rotif_m","clad"))
##     region   var propmissing  propzeros exclude
## 1 Far West  clad 0.141025641 0.95149254    TRUE
## 2 Far West mysid 0.137820513 0.21933086    TRUE
## 3    North  clad 0.012820513 0.14610390   FALSE
## 4    South mysid 0.006410256 0.08709677    TRUE
## 5     West  clad 0.009615385 0.26537217   FALSE
filter(dataavail,exclude) %>% filter(var %in% c("marfish_bsmt","potam","corbic"))
##     region          var propmissing propzeros exclude
## 1 Far West       corbic   0.1410256 1.0000000    TRUE
## 2    North marfish_bsmt   0.1891026 0.9762846    TRUE
## 3    North        potam   0.1378205 0.1486989    TRUE
## 4    South marfish_bsmt   0.1826923 1.0000000    TRUE
## 5    South        potam   0.1378205 0.9702602    TRUE
## 6     West       corbic   0.1378205 0.8066914    TRUE
## 7     West marfish_bsmt   0.1666667 0.2192308    TRUE

Time series plots

## Note: Using an external vector in selections is ambiguous.
## i Use `all_of(varnames)` instead of `varnames` to silence this message.
## i See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This message is displayed once per session.

Other useful plots

Breakdown of total zooplankton biomass.

## Warning: Removed 272 rows containing missing values (position_stack).

Correlation between biomass and energy.

for(i in 1:length(regions)) {
  dtemp=filter(fdr,region==regions[i])
  print(regions[i])
  print(cor(dtemp$tzoop,dtemp$tzoop_e,use = "p"))
  print(cor(dtemp$hzoop,dtemp$hzoop_e,use = "p"))
  print(cor(dtemp$pzoop,dtemp$pzoop_e,use = "p"))
}
## [1] "Far West"
##           [,1]
## [1,] 0.9967037
##           [,1]
## [1,] 0.9969857
##           [,1]
## [1,] 0.9996106
## [1] "North"
##           [,1]
## [1,] 0.9958978
##           [,1]
## [1,] 0.9945197
##          [,1]
## [1,] 0.999494
## [1] "South"
##           [,1]
## [1,] 0.9967635
##           [,1]
## [1,] 0.9965366
##         [,1]
## [1,] 0.99925
## [1] "West"
##           [,1]
## [1,] 0.9964103
##          [,1]
## [1,] 0.994996
##           [,1]
## [1,] 0.9983814

Cross-correlation matrices

(only sig correlations shown… no correction for multiple comparisons)

## Warning: `expand_scale()` is deprecated; use `expansion()` instead.

## Warning: `expand_scale()` is deprecated; use `expansion()` instead.

## Warning: `expand_scale()` is deprecated; use `expansion()` instead.

## Warning: `expand_scale()` is deprecated; use `expansion()` instead.

## Warning: `expand_scale()` is deprecated; use `expansion()` instead.

## Warning: `expand_scale()` is deprecated; use `expansion()` instead.

Other notes:

Detrended fish indices are NOT correlated in S!

Nitrate and ammonia are positively correlated, max at lag 0 all regions.
Nitrate and dophos are positively correlated, max at lag 0 all regions.
Ammonia and dophos are positively correlated, lag 0 for FW and S, ammonia lags dphos by 3 months in W and N.

Chla nitrate neg correlated, lag 0.
Chla ammonia neg correlated, lag 0.
Chla dophos relationship unclear.

High flow 2-4 month prev = high chla

Hcope lags chla by 1, positive, except FW.
Clad seem to precede chla by 2, positive.
Amphi relationship unclear, prob bc not eating chla in water column.
In N and W, chla lags potam, negative. The opposite in W.

Mysid and hcope postive, lag 0.
In S and W, hcope lags pcope, negative.

Exploratory plots

Fish-centered model (upper trophic level aggregates)

modFW='hzoop~hb1*chla_1+hs1*hzoop_1+ht1*pzoop_1+ht2*potam_1+ht3*estfish_bsmt_1+ha1*flow+ha2*temp+ha3*turbid
        pzoop~pb1*hzoop_1+ps1*pzoop_1+pt1*potam_1+pt2*estfish_bsmt_1+pa1*flow+pa2*temp+pa3*turbid
        estfish_bsmt~fb1*hzoop_1+fb2*pzoop_1+fs1*estfish_bsmt_1+fa1*flow+fa2*temp+fa3*turbid+ft1*marfish_bsmt_1
        
        hb:=sqrt(hb1^2)
        hs:=sqrt(hs1^2)
        ht:=sqrt(ht1^2+ht2^2+ht3^2)
        ha:=sqrt(ha1^2+ha2^2-ha3^2)
        
        pb:=sqrt(pb1^2)
        ps:=sqrt(ps1^2)
        pt:=sqrt(pt1^2+pt2^2)
        pa:=sqrt(pa1^2+pa2^2+pa3^2)
        
        fb:=sqrt(fb1^2+fb2^2)
        fs:=sqrt(fs1^2)
        ft:=sqrt(ft1^2)
        fa:=sqrt(fa1^2+fa2^2+fa3^2)
'

modW='hzoop~hb1*chla_1+hs1*hzoop_1+ht1*pzoop_1+ht2*potam_1+ht3*estfish_bsmt_1+ha1*flow+ha2*temp+ha3*turbid
        pzoop~pb1*chla_1+pb2*hzoop_1+ps1*pzoop_1+pt1*potam_1+pt2*estfish_bsmt_1+pa1*flow+pa2*temp+pa3*turbid
        estfish_bsmt~fb1*hzoop_1+fb2*pzoop_1+fs1*estfish_bsmt_1+fa1*flow+fa2*temp+fa3*turbid
        
        hb:=sqrt(hb1^2)
        hs:=sqrt(hs1^2)
        ht:=sqrt(ht1^2+ht2^2+ht3^2)
        ha:=sqrt(ha1^2+ha2^2-ha3^2)
        
        pb:=sqrt(pb1^2+pb2^2)
        ps:=sqrt(ps1^2)
        pt:=sqrt(pt1^2+pt2^2)
        pa:=sqrt(pa1^2+pa2^2+pa3^2)
        
        fb:=sqrt(fb1^2+fb2^2)
        fs:=sqrt(fs1^2)
        fa:=sqrt(fa1^2+fa2^2+fa3^2)
'

modN='hzoop~hb1*chla_1+hs1*hzoop_1+ht1*pzoop_1+ht2*corbic_1+ht3*estfish_bsmt_1+ha1*flow+ha2*temp+ha3*turbid
        pzoop~pb1*chla_1+pb2*hzoop_1+ps1*pzoop_1+pt1*corbic_1+pt2*estfish_bsmt_1+pa1*flow+pa2*temp+pa3*turbid
        estfish_bsmt~fb1*hzoop_1+fb2*pzoop_1+fs1*estfish_bsmt_1+fa1*flow+fa2*temp+fa3*turbid+ft1*sside_1+ft2*cent_1
        
        hb:=sqrt(hb1^2)
        hs:=sqrt(hs1^2)
        ht:=sqrt(ht1^2+ht2^2+ht3^2)
        ha:=sqrt(ha1^2+ha2^2-ha3^2)
        
        pb:=sqrt(pb1^2+pb2^2)
        ps:=sqrt(ps1^2)
        pt:=sqrt(pt1^2+pt2^2)
        pa:=sqrt(pa1^2+pa2^2+pa3^2)
        
        fb:=sqrt(fb1^2+fb2^2)
        fs:=sqrt(fs1^2)
        ft:=sqrt(ft1^2+ft2^2)
        fa:=sqrt(fa1^2+fa2^2+fa3^2)
'

modS='hzoop~hb1*chla_1+hs1*hzoop_1+ht1*pzoop_1+ht2*corbic_1+ht3*estfish_bsmt_1+ha1*flow+ha2*temp+ha3*turbid
        pzoop~pb1*chla_1+pb2*hzoop_1+ps1*pzoop_1+pt1*corbic_1+pt2*estfish_bsmt_1+pa1*flow+pa2*temp+pa3*turbid
        estfish_bsmt~fb1*chla_1+fb2*hzoop_1+fb3*pzoop_1+fs1*estfish_bsmt_1+fa1*flow+fa2*temp+fa3*turbid+ft1*sside_1+ft2*cent_1
        
        hb:=sqrt(hb1^2)
        hs:=sqrt(hs1^2)
        ht:=sqrt(ht1^2+ht2^2+ht3^2)
        ha:=sqrt(ha1^2+ha2^2-ha3^2)
        
        pb:=sqrt(pb1^2+pb2^2)
        ps:=sqrt(ps1^2)
        pt:=sqrt(pt1^2+pt2^2)
        pa:=sqrt(pa1^2+pa2^2+pa3^2)
        
        fb:=sqrt(fb1^2+fb2^2+fb3^2)
        fs:=sqrt(fs1^2)
        ft:=sqrt(ft1^2+ft2^2)
        fa:=sqrt(fa1^2+fa2^2+fa3^2)
'

modfitFW=sem(modFW, data=filter(fdr_ds,region=="Far West"))
modfitW=sem(modW, data=filter(fdr_ds,region=="West"))
modfitN=sem(modN, data=filter(fdr_ds,region=="North"))
modfitS=sem(modS, data=filter(fdr_ds,region=="South"))
summary(modfitFW, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 18 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        28
##                                                       
##                                                   Used       Total
##   Number of observations                           191         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 6.605
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.252
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   hzoop ~                                                               
##     chla_1   (hb1)    0.030    0.076    0.392    0.695    0.030    0.025
##     hzoop_1  (hs1)    0.316    0.065    4.863    0.000    0.316    0.327
##     pzoop_1  (ht1)    0.054    0.062    0.871    0.384    0.054    0.057
##     potam_1  (ht2)   -0.187    0.058   -3.214    0.001   -0.187   -0.210
##     estfs__1 (ht3)   -0.173    0.069   -2.519    0.012   -0.173   -0.172
##     flow     (ha1)   -0.018    0.077   -0.235    0.815   -0.018   -0.016
##     temp     (ha2)   -0.090    0.072   -1.247    0.212   -0.090   -0.085
##     turbid   (ha3)    0.044    0.080    0.548    0.584    0.044    0.039
##   pzoop ~                                                               
##     hzoop_1  (pb1)    0.050    0.067    0.744    0.457    0.050    0.049
##     pzoop_1  (ps1)    0.340    0.064    5.295    0.000    0.340    0.344
##     potam_1  (pt1)   -0.098    0.060   -1.640    0.101   -0.098   -0.105
##     estfs__1 (pt2)   -0.034    0.071   -0.475    0.635   -0.034   -0.032
##     flow     (pa1)    0.159    0.080    1.990    0.047    0.159    0.138
##     temp     (pa2)   -0.020    0.075   -0.271    0.786   -0.020   -0.018
##     turbid   (pa3)    0.225    0.083    2.718    0.007    0.225    0.193
##   estfish_bsmt ~                                                        
##     hzoop_1  (fb1)   -0.184    0.056   -3.270    0.001   -0.184   -0.198
##     pzoop_1  (fb2)    0.118    0.055    2.142    0.032    0.118    0.131
##     estfs__1 (fs1)    0.341    0.060    5.668    0.000    0.341    0.353
##     flow     (fa1)    0.096    0.069    1.395    0.163    0.096    0.091
##     temp     (fa2)   -0.034    0.064   -0.527    0.598   -0.034   -0.033
##     turbid   (fa3)    0.245    0.071    3.475    0.001    0.245    0.228
##     mrfsh__1 (ft1)    0.001    0.063    0.015    0.988    0.001    0.001
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .hzoop ~~                                                              
##    .pzoop            -0.029    0.058   -0.508    0.612   -0.029   -0.037
##    .estfish_bsmt     -0.048    0.050   -0.971    0.331   -0.048   -0.070
##  .pzoop ~~                                                              
##    .estfish_bsmt     -0.093    0.052   -1.805    0.071   -0.093   -0.132
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hzoop             0.775    0.079    9.772    0.000    0.775    0.755
##    .pzoop             0.830    0.085    9.772    0.000    0.830    0.739
##    .estfish_bsmt      0.603    0.062    9.772    0.000    0.603    0.639
## 
## R-Square:
##                    Estimate
##     hzoop             0.245
##     pzoop             0.261
##     estfish_bsmt      0.361
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     hb                0.030    0.076    0.392    0.695    0.030    0.025
##     hs                0.316    0.065    4.863    0.000    0.316    0.327
##     ht                0.260    0.057    4.533    0.000    0.260    0.277
##     ha                0.080    0.098    0.823    0.411    0.080    0.077
##     pb                0.050    0.067    0.744    0.457    0.050    0.049
##     ps                0.340    0.064    5.295    0.000    0.340    0.344
##     pt                0.104    0.057    1.802    0.072    0.104    0.110
##     pa                0.276    0.066    4.175    0.000    0.276    0.237
##     fb                0.218    0.058    3.757    0.000    0.218    0.237
##     fs                0.341    0.060    5.668    0.000    0.341    0.353
##     ft                0.001    0.063    0.015    0.988    0.001    0.001
##     fa                0.266    0.060    4.427    0.000    0.266    0.248
summary(modfitW, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 23 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        28
##                                                       
##                                                   Used       Total
##   Number of observations                           210         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 0.042
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.979
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   hzoop ~                                                               
##     chla_1   (hb1)    0.052    0.067    0.770    0.441    0.052    0.047
##     hzoop_1  (hs1)    0.430    0.069    6.227    0.000    0.430    0.413
##     pzoop_1  (ht1)    0.012    0.063    0.184    0.854    0.012    0.011
##     potam_1  (ht2)   -0.195    0.067   -2.914    0.004   -0.195   -0.182
##     estfs__1 (ht3)    0.017    0.062    0.279    0.781    0.017    0.017
##     flow     (ha1)    0.236    0.070    3.370    0.001    0.236    0.203
##     temp     (ha2)    0.009    0.061    0.142    0.887    0.009    0.009
##     turbid   (ha3)   -0.176    0.068   -2.609    0.009   -0.176   -0.163
##   pzoop ~                                                               
##     chla_1   (pb1)    0.196    0.065    3.029    0.002    0.196    0.182
##     hzoop_1  (pb2)    0.104    0.067    1.557    0.119    0.104    0.103
##     pzoop_1  (ps1)    0.423    0.061    6.888    0.000    0.423    0.419
##     potam_1  (pt1)   -0.088    0.064   -1.361    0.174   -0.088   -0.084
##     estfs__1 (pt2)    0.040    0.060    0.656    0.512    0.040    0.039
##     flow     (pa1)   -0.112    0.068   -1.651    0.099   -0.112   -0.100
##     temp     (pa2)    0.187    0.059    3.168    0.002    0.187    0.190
##     turbid   (pa3)    0.027    0.065    0.411    0.681    0.027    0.026
##   estfish_bsmt ~                                                        
##     hzoop_1  (fb1)    0.106    0.070    1.501    0.133    0.106    0.104
##     pzoop_1  (fb2)   -0.073    0.069   -1.044    0.296   -0.073   -0.071
##     estfs__1 (fs1)    0.278    0.068    4.089    0.000    0.278    0.270
##     flow     (fa1)   -0.229    0.076   -2.997    0.003   -0.229   -0.202
##     temp     (fa2)    0.037    0.066    0.561    0.575    0.037    0.037
##     turbid   (fa3)    0.242    0.071    3.396    0.001    0.242    0.228
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .hzoop ~~                                                              
##    .pzoop             0.181    0.049    3.692    0.000    0.181    0.263
##    .estfish_bsmt     -0.073    0.054   -1.354    0.176   -0.073   -0.094
##  .pzoop ~~                                                              
##    .estfish_bsmt      0.127    0.053    2.412    0.016    0.127    0.169
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hzoop             0.709    0.069   10.247    0.000    0.709    0.666
##    .pzoop             0.666    0.065   10.247    0.000    0.666    0.667
##    .estfish_bsmt      0.854    0.083   10.247    0.000    0.854    0.835
## 
## R-Square:
##                    Estimate
##     hzoop             0.334
##     pzoop             0.333
##     estfish_bsmt      0.165
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     hb                0.052    0.067    0.770    0.441    0.052    0.047
##     hs                0.430    0.069    6.227    0.000    0.430    0.413
##     ht                0.197    0.066    2.966    0.003    0.197    0.183
##     ha                0.157    0.118    1.327    0.185    0.157    0.123
##     pb                0.221    0.057    3.889    0.000    0.221    0.209
##     ps                0.423    0.061    6.888    0.000    0.423    0.419
##     pt                0.096    0.061    1.567    0.117    0.096    0.093
##     pa                0.220    0.062    3.548    0.000    0.220    0.216
##     fb                0.128    0.079    1.620    0.105    0.128    0.126
##     fs                0.278    0.068    4.089    0.000    0.278    0.270
##     fa                0.335    0.081    4.123    0.000    0.335    0.306
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 19 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        30
##                                                       
##                                                   Used       Total
##   Number of observations                           193         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 4.536
##   Degrees of freedom                                 6
##   P-value (Chi-square)                           0.604
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   hzoop ~                                                               
##     chla_1   (hb1)    0.025    0.071    0.353    0.724    0.025    0.023
##     hzoop_1  (hs1)    0.200    0.069    2.899    0.004    0.200    0.195
##     pzoop_1  (ht1)    0.033    0.071    0.466    0.641    0.033    0.033
##     corbic_1 (ht2)    0.020    0.067    0.296    0.767    0.020    0.019
##     estfs__1 (ht3)   -0.054    0.067   -0.798    0.425   -0.054   -0.057
##     flow     (ha1)    0.330    0.077    4.275    0.000    0.330    0.309
##     temp     (ha2)    0.097    0.064    1.511    0.131    0.097    0.100
##     turbid   (ha3)    0.177    0.069    2.574    0.010    0.177    0.172
##   pzoop ~                                                               
##     chla_1   (pb1)    0.255    0.068    3.747    0.000    0.255    0.235
##     hzoop_1  (pb2)    0.134    0.066    2.043    0.041    0.134    0.132
##     pzoop_1  (ps1)    0.283    0.068    4.163    0.000    0.283    0.279
##     corbic_1 (pt1)    0.009    0.064    0.138    0.890    0.009    0.009
##     estfs__1 (pt2)   -0.030    0.064   -0.463    0.643   -0.030   -0.032
##     flow     (pa1)   -0.295    0.074   -4.008    0.000   -0.295   -0.278
##     temp     (pa2)    0.127    0.061    2.079    0.038    0.127    0.132
##     turbid   (pa3)   -0.010    0.066   -0.147    0.883   -0.010   -0.009
##   estfish_bsmt ~                                                        
##     hzoop_1  (fb1)    0.148    0.073    2.025    0.043    0.148    0.135
##     pzoop_1  (fb2)    0.043    0.073    0.593    0.553    0.043    0.040
##     estfs__1 (fs1)    0.127    0.070    1.822    0.068    0.127    0.127
##     flow     (fa1)   -0.518    0.080   -6.484    0.000   -0.518   -0.454
##     temp     (fa2)    0.030    0.066    0.455    0.649    0.030    0.029
##     turbid   (fa3)    0.104    0.071    1.462    0.144    0.104    0.094
##     sside_1  (ft1)   -0.014    0.067   -0.214    0.831   -0.014   -0.014
##     cent_1   (ft2)   -0.149    0.068   -2.200    0.028   -0.149   -0.141
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .hzoop ~~                                                              
##    .pzoop             0.169    0.055    3.063    0.002    0.169    0.226
##    .estfish_bsmt     -0.030    0.058   -0.517    0.605   -0.030   -0.037
##  .pzoop ~~                                                              
##    .estfish_bsmt      0.050    0.055    0.900    0.368    0.050    0.065
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hzoop             0.781    0.079    9.823    0.000    0.781    0.787
##    .pzoop             0.712    0.072    9.823    0.000    0.712    0.725
##    .estfish_bsmt      0.820    0.083    9.823    0.000    0.820    0.722
## 
## R-Square:
##                    Estimate
##     hzoop             0.213
##     pzoop             0.275
##     estfish_bsmt      0.278
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     hb                0.025    0.071    0.353    0.724    0.025    0.023
##     hs                0.200    0.069    2.899    0.004    0.200    0.195
##     ht                0.066    0.076    0.866    0.387    0.066    0.069
##     ha                0.295    0.106    2.769    0.006    0.295    0.276
##     pb                0.289    0.066    4.358    0.000    0.289    0.270
##     ps                0.283    0.068    4.163    0.000    0.283    0.279
##     pt                0.031    0.065    0.474    0.636    0.031    0.033
##     pa                0.322    0.069    4.661    0.000    0.322    0.308
##     fb                0.154    0.068    2.262    0.024    0.154    0.141
##     fs                0.127    0.070    1.822    0.068    0.127    0.127
##     ft                0.150    0.067    2.219    0.026    0.150    0.142
##     fa                0.529    0.081    6.498    0.000    0.529    0.465
summary(modfitS, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 18 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        31
##                                                       
##                                                   Used       Total
##   Number of observations                           199         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 5.034
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.412
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   hzoop ~                                                               
##     chla_1   (hb1)    0.244    0.074    3.301    0.001    0.244    0.225
##     hzoop_1  (hs1)    0.215    0.070    3.057    0.002    0.215    0.205
##     pzoop_1  (ht1)   -0.025    0.078   -0.322    0.748   -0.025   -0.023
##     corbic_1 (ht2)    0.094    0.071    1.311    0.190    0.094    0.087
##     estfs__1 (ht3)   -0.012    0.070   -0.174    0.862   -0.012   -0.012
##     flow     (ha1)    0.140    0.076    1.834    0.067    0.140    0.122
##     temp     (ha2)    0.180    0.068    2.636    0.008    0.180    0.177
##     turbid   (ha3)   -0.037    0.069   -0.540    0.589   -0.037   -0.036
##   pzoop ~                                                               
##     chla_1   (pb1)    0.303    0.061    4.964    0.000    0.303    0.302
##     hzoop_1  (pb2)    0.135    0.058    2.320    0.020    0.135    0.139
##     pzoop_1  (ps1)    0.328    0.064    5.116    0.000    0.328    0.324
##     corbic_1 (pt1)   -0.066    0.058   -1.134    0.257   -0.066   -0.066
##     estfs__1 (pt2)    0.028    0.058    0.490    0.624    0.028    0.030
##     flow     (pa1)   -0.102    0.063   -1.627    0.104   -0.102   -0.097
##     temp     (pa2)   -0.042    0.057   -0.740    0.459   -0.042   -0.044
##     turbid   (pa3)    0.092    0.057    1.610    0.107    0.092    0.096
##   estfish_bsmt ~                                                        
##     chla_1   (fb1)    0.141    0.070    2.000    0.045    0.141    0.135
##     hzoop_1  (fb2)    0.144    0.067    2.166    0.030    0.144    0.143
##     pzoop_1  (fb3)   -0.043    0.074   -0.575    0.565   -0.043   -0.041
##     estfs__1 (fs1)    0.212    0.067    3.158    0.002    0.212    0.217
##     flow     (fa1)   -0.037    0.073   -0.510    0.610   -0.037   -0.034
##     temp     (fa2)   -0.033    0.065   -0.511    0.609   -0.033   -0.034
##     turbid   (fa3)    0.217    0.069    3.164    0.002    0.217    0.219
##     sside_1  (ft1)    0.055    0.065    0.846    0.398    0.055    0.055
##     cent_1   (ft2)   -0.078    0.067   -1.166    0.244   -0.078   -0.081
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .hzoop ~~                                                              
##    .pzoop             0.099    0.052    1.899    0.058    0.099    0.136
##    .estfish_bsmt      0.031    0.060    0.515    0.606    0.031    0.037
##  .pzoop ~~                                                              
##    .estfish_bsmt      0.139    0.050    2.767    0.006    0.139    0.200
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hzoop             0.883    0.088    9.975    0.000    0.883    0.843
##    .pzoop             0.602    0.060    9.975    0.000    0.602    0.670
##    .estfish_bsmt      0.797    0.080    9.975    0.000    0.797    0.824
## 
## R-Square:
##                    Estimate
##     hzoop             0.157
##     pzoop             0.330
##     estfish_bsmt      0.176
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     hb                0.244    0.074    3.301    0.001    0.244    0.225
##     hs                0.215    0.070    3.057    0.002    0.215    0.205
##     ht                0.098    0.072    1.364    0.173    0.098    0.091
##     ha                0.225    0.076    2.959    0.003    0.225    0.212
##     pb                0.331    0.058    5.696    0.000    0.331    0.332
##     ps                0.328    0.064    5.116    0.000    0.328    0.324
##     pt                0.072    0.057    1.257    0.209    0.072    0.072
##     pa                0.144    0.065    2.232    0.026    0.144    0.144
##     fb                0.206    0.070    2.959    0.003    0.206    0.201
##     fs                0.212    0.067    3.158    0.002    0.212    0.217
##     ft                0.095    0.060    1.578    0.115    0.095    0.098
##     fa                0.223    0.070    3.173    0.002    0.223    0.224
#modificationindices(modfitW, sort=T, maximum.number=20)
#residuals(modfitS)

labelsfarwest=createLabels(modfitFW, cnameslag)
labelswest=createLabels(modfitW, cnameslag)
labelsnorth=createLabels(modfitN, cnameslag)
labelssouth=createLabels(modfitS, cnameslag)

#FAR WEST
myLavaanPlot(model=modfitFW, labels=labelsfarwest,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#WEST
myLavaanPlot(model=modfitW, labels=labelswest,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#NORTH
myLavaanPlot(model=modfitN, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#SOUTH
myLavaanPlot(model=modfitS, labels=labelssouth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#FAR WEST
generateInputDf(fit=modfitFW, reference_df=cnameslag) %>%
  getUpperTrophicCoord() %>%
  createGraph(fit=modfitFW) %>%
  DiagrammeR::render_graph(title="Far West")
#WEST
generateInputDf(fit=modfitW, reference_df=cnameslag) %>%
  getUpperTrophicCoord() %>%
  createGraph(fit=modfitW) %>%
  DiagrammeR::render_graph(title="West")
#NORTH
generateInputDf(fit=modfitN, reference_df=cnameslag) %>%
  getUpperTrophicCoord() %>%
  createGraph(fit=modfitN) %>%
  DiagrammeR::render_graph(title="North")
#SOUTH
generateInputDf(fit=modfitS, reference_df=cnameslag) %>%
  getUpperTrophicCoord() %>%
  createGraph(fit=modfitS) %>%
  DiagrammeR::render_graph(title="South")

Total effects

ssFW=standardizedsolution(modfitFW) %>% mutate(region="Far West")
ssW=standardizedsolution(modfitW) %>% mutate(region="West")
ssN=standardizedsolution(modfitN) %>% mutate(region="North")
ssS=standardizedsolution(modfitS) %>% mutate(region="South")

ssut=rbind(ssFW,ssW,ssN,ssS) %>% filter(op==":=") %>% select(region,lhs,est.std:ci.upper) %>% 
  separate(lhs,c("variable","influence"), sep=1) %>% 
  mutate(variable=case_when(variable=="h" ~ "herbivorous\nzooplankton",
                            variable=="p" ~ "predatory\nzooplankton",
                            variable=="f" ~ "estuarine\nfishes"),
         influence=case_when(influence=="b" ~ "bottom-up",
                            influence=="t" ~ "top-down",
                            influence=="s" ~ "self-regulation",
                            influence=="a" ~ "abiotic drivers"),
         region=factor(region, levels=regionorder),
         influence=factor(influence, levels=c("self-regulation","bottom-up","top-down","abiotic drivers")),
         variable=factor(variable,levels=c("estuarine\nfishes","predatory\nzooplankton","herbivorous\nzooplankton")),
         sig=ifelse(pvalue<0.05,"*",""))

ggplot(ssut,aes(x=influence,y=est.std)) +
  facet_grid(variable~region) +
  geom_errorbar(aes(ymin=ci.lower, ymax=ci.upper),width=0.5) +
  geom_point() +
  geom_text(aes(y=ci.upper+0.05, label=sig)) +
  geom_hline(yintercept = 0) +
  theme_bw() + theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1)) +
  labs(y="total effect (standardized)")

#ggsave("../uteffects.png",width = 6,height=5)

Phytoplankton-centered model (lower trophic level aggregates)

modFW='din~ns1*din_1+nt1*chla+nn1*hzoop_1+nn2*pzoop_1+nn3*potam_1+na1*flow+na2*temp+na3*turbid
        chla~cb1*din_1+cs1*chla_1+ct1*hzoop_1+ct2*potam_1+ca1*flow+ca2*temp+ca3*turbid
        potam~lb1*chla_1+lb2*hzoop_1+lb3*pzoop_1+ls1*potam_1+la1*flow+la2*temp+la3*turbid
        
        ns:=sqrt(ns1^2)
        nt:=sqrt(nt1^2)
        nn:=sqrt(nn1^2+nn2^2+nn3^2)
        na:=sqrt(na1^2+na2^2+na3^2)
        
        cb:=sqrt(cb1^2)
        cs:=sqrt(cs1^2)
        ct:=sqrt(ct1^2+ct2^2)
        ca:=sqrt(ca1^2+ca2^2+ca3^2)
        
        lb:=sqrt(lb1^2+lb2^2+lb3^2)
        ls:=sqrt(ls1^2)
        la:=sqrt(la1^2+la2^2+la3^2)
'

modW='din~ns1*din_1+nt1*chla+nn1*hzoop_1+nn2*pzoop_1+nn3*potam_1+na1*flow+na2*temp+na3*turbid
        chla~cb1*din_1+cs1*chla_1+ct1*hzoop_1+ct2*potam_1+ca1*flow+ca2*temp+ca3*turbid
        potam~lb1*din_1+lb2*chla_1+lb3*hzoop_1+lb4*pzoop_1+ls1*potam_1+la1*flow+la2*temp+la3*turbid
        
        ns:=sqrt(ns1^2)
        nt:=sqrt(nt1^2)
        nn:=sqrt(nn1^2+nn2^2+nn3^2)
        na:=sqrt(na1^2+na2^2+na3^2)
        
        cb:=sqrt(cb1^2)
        cs:=sqrt(cs1^2)
        ct:=sqrt(ct1^2+ct2^2)
        ca:=sqrt(ca1^2+ca2^2+ca3^2)
        
        lb:=sqrt(lb1^2+lb2^2+lb3^2+lb4^2)
        ls:=sqrt(ls1^2)
        la:=sqrt(la1^2+la2^2+la3^2)
'

modN='din~ns1*din_1+nt1*chla+nn1*hzoop_1+nn2*pzoop_1+nn3*corbic_1+na1*flow+na2*temp+na3*turbid
        chla~cb1*din_1+cs1*chla_1+ct1*hzoop_1+ct2*corbic_1+ct3*pzoop_1+ca1*flow+ca2*temp+ca3*turbid
        corbic~lb1*chla_1+lb2*hzoop_1+lb3*pzoop_1+ls1*corbic_1+la1*flow+la2*temp+la3*turbid
        
        ns:=sqrt(ns1^2)
        nt:=sqrt(nt1^2)
        nn:=sqrt(nn1^2+nn2^2+nn3^2)
        na:=sqrt(na1^2+na2^2+na3^2)
        
        cb:=sqrt(cb1^2)
        cs:=sqrt(cs1^2)
        ct:=sqrt(ct1^2+ct2^2+ct3^2)
        ca:=sqrt(ca1^2+ca2^2+ca3^2)
        
        lb:=sqrt(lb1^2+lb2^2+lb3^2)
        ls:=sqrt(ls1^2)
        la:=sqrt(la1^2+la2^2+la3^2)
'

modS='din~ns1*din_1+nt1*chla+nn1*hzoop_1+nn2*pzoop_1+nn3*corbic_1+na1*flow+na2*temp+na3*turbid
        chla~cb1*din_1+cs1*chla_1+ct1*hzoop_1+ct2*corbic_1+ca1*flow+ca2*temp+ca3*turbid
        corbic~lb1*chla_1+lb2*hzoop_1+lb3*pzoop_1+ls1*corbic_1+la1*flow+la2*temp+la3*turbid
        
        ns:=sqrt(ns1^2)
        nt:=sqrt(nt1^2)
        nn:=sqrt(nn1^2+nn2^2+nn3^2)
        na:=sqrt(na1^2+na2^2+na3^2)
        
        cb:=sqrt(cb1^2)
        cs:=sqrt(cs1^2)
        ct:=sqrt(ct1^2+ct2^2)
        ca:=sqrt(ca1^2+ca2^2+ca3^2)
        
        lb:=sqrt(lb1^2+lb2^2+lb3^2)
        ls:=sqrt(ls1^2)
        la:=sqrt(la1^2+la2^2+la3^2)
'

modfitFW=sem(modFW, data=filter(fdr_ds,region=="Far West"))
modfitW=sem(modW, data=filter(fdr_ds,region=="West"))
modfitN=sem(modN, data=filter(fdr_ds,region=="North"))
modfitS=sem(modS, data=filter(fdr_ds,region=="South"))
summary(modfitFW, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 19 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        26
##                                                       
##                                                   Used       Total
##   Number of observations                           234         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 1.283
##   Degrees of freedom                                 4
##   P-value (Chi-square)                           0.864
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   din ~                                                                 
##     din_1    (ns1)    0.425    0.057    7.426    0.000    0.425    0.425
##     chla     (nt1)   -0.163    0.071   -2.280    0.023   -0.163   -0.130
##     hzoop_1  (nn1)    0.033    0.059    0.550    0.582    0.033    0.033
##     pzoop_1  (nn2)   -0.020    0.058   -0.343    0.731   -0.020   -0.020
##     potam_1  (nn3)   -0.006    0.057   -0.101    0.919   -0.006   -0.006
##     flow     (na1)   -0.038    0.072   -0.522    0.602   -0.038   -0.032
##     temp     (na2)   -0.156    0.064   -2.449    0.014   -0.156   -0.150
##     turbid   (na3)   -0.131    0.068   -1.915    0.056   -0.131   -0.123
##   chla ~                                                                
##     din_1    (cb1)    0.011    0.051    0.219    0.827    0.011    0.014
##     chla_1   (cs1)    0.258    0.063    4.091    0.000    0.258    0.262
##     hzoop_1  (ct1)   -0.013    0.053   -0.241    0.809   -0.013   -0.016
##     potam_1  (ct2)   -0.016    0.050   -0.320    0.749   -0.016   -0.021
##     flow     (ca1)    0.006    0.063    0.103    0.918    0.006    0.007
##     temp     (ca2)    0.122    0.056    2.182    0.029    0.122    0.146
##     turbid   (ca3)   -0.027    0.060   -0.453    0.651   -0.027   -0.032
##   potam ~                                                               
##     chla_1   (lb1)    0.069    0.061    1.132    0.258    0.069    0.054
##     hzoop_1  (lb2)   -0.083    0.051   -1.624    0.104   -0.083   -0.081
##     pzoop_1  (lb3)   -0.149    0.050   -2.983    0.003   -0.149   -0.147
##     potam_1  (ls1)    0.633    0.049   13.014    0.000    0.633    0.633
##     flow     (la1)    0.049    0.062    0.785    0.432    0.049    0.041
##     temp     (la2)   -0.048    0.055   -0.888    0.375   -0.048   -0.045
##     turbid   (la3)    0.059    0.059    1.012    0.312    0.059    0.054
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .din ~~                                                                
##    .potam            -0.044    0.044   -1.007    0.314   -0.044   -0.066
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .din               0.777    0.072   10.817    0.000    0.777    0.753
##    .chla              0.605    0.056   10.817    0.000    0.605    0.913
##    .potam             0.575    0.053   10.817    0.000    0.575    0.524
## 
## R-Square:
##                    Estimate
##     din               0.247
##     chla              0.087
##     potam             0.476
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     ns                0.425    0.057    7.426    0.000    0.425    0.425
##     nt                0.163    0.071    2.280    0.023    0.163    0.130
##     nn                0.039    0.060    0.641    0.521    0.039    0.039
##     na                0.207    0.071    2.925    0.003    0.207    0.196
##     cb                0.011    0.051    0.219    0.827    0.011    0.014
##     cs                0.258    0.063    4.091    0.000    0.258    0.262
##     ct                0.020    0.056    0.364    0.716    0.020    0.026
##     ca                0.125    0.053    2.357    0.018    0.125    0.150
##     lb                0.184    0.050    3.679    0.000    0.184    0.176
##     ls                0.633    0.049   13.014    0.000    0.633    0.633
##     la                0.091    0.044    2.067    0.039    0.091    0.081
summary(modfitW, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 25 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        27
##                                                       
##                                                   Used       Total
##   Number of observations                           257         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 6.563
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.087
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   din ~                                                                 
##     din_1    (ns1)    0.457    0.052    8.819    0.000    0.457    0.456
##     chla     (nt1)   -0.138    0.053   -2.604    0.009   -0.138   -0.124
##     hzoop_1  (nn1)   -0.021    0.051   -0.413    0.679   -0.021   -0.022
##     pzoop_1  (nn2)    0.059    0.049    1.201    0.230    0.059    0.059
##     potam_1  (nn3)    0.099    0.054    1.851    0.064    0.099    0.100
##     flow     (na1)   -0.308    0.054   -5.758    0.000   -0.308   -0.285
##     temp     (na2)    0.024    0.048    0.494    0.622    0.024    0.024
##     turbid   (na3)    0.092    0.051    1.800    0.072    0.092    0.092
##   chla ~                                                                
##     din_1    (cb1)   -0.128    0.061   -2.117    0.034   -0.128   -0.142
##     chla_1   (cs1)    0.148    0.065    2.297    0.022    0.148    0.149
##     hzoop_1  (ct1)    0.095    0.058    1.651    0.099    0.095    0.109
##     potam_1  (ct2)    0.033    0.062    0.536    0.592    0.033    0.037
##     flow     (ca1)    0.067    0.062    1.094    0.274    0.067    0.069
##     temp     (ca2)   -0.057    0.055   -1.044    0.297   -0.057   -0.064
##     turbid   (ca3)   -0.030    0.059   -0.514    0.607   -0.030   -0.034
##   potam ~                                                               
##     din_1    (lb1)    0.091    0.043    2.118    0.034    0.091    0.090
##     chla_1   (lb2)   -0.007    0.046   -0.151    0.880   -0.007   -0.006
##     hzoop_1  (lb3)   -0.044    0.043   -1.029    0.304   -0.044   -0.045
##     pzoop_1  (lb4)    0.085    0.040    2.106    0.035    0.085    0.084
##     potam_1  (ls1)    0.711    0.044   16.154    0.000    0.711    0.705
##     flow     (la1)   -0.071    0.044   -1.625    0.104   -0.071   -0.065
##     temp     (la2)   -0.011    0.039   -0.272    0.785   -0.011   -0.011
##     turbid   (la3)   -0.087    0.042   -2.086    0.037   -0.087   -0.086
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .din ~~                                                                
##    .potam             0.048    0.028    1.741    0.082    0.048    0.109
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .din               0.542    0.048   11.336    0.000    0.542    0.544
##    .chla              0.724    0.064   11.336    0.000    0.724    0.898
##    .potam             0.363    0.032   11.336    0.000    0.363    0.355
## 
## R-Square:
##                    Estimate
##     din               0.456
##     chla              0.102
##     potam             0.645
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     ns                0.457    0.052    8.819    0.000    0.457    0.456
##     nt                0.138    0.053    2.604    0.009    0.138    0.124
##     nn                0.117    0.054    2.191    0.028    0.117    0.118
##     na                0.322    0.055    5.814    0.000    0.322    0.300
##     cb                0.128    0.061    2.117    0.034    0.128    0.142
##     cs                0.148    0.065    2.297    0.022    0.148    0.149
##     ct                0.101    0.063    1.615    0.106    0.101    0.115
##     ca                0.093    0.061    1.534    0.125    0.093    0.100
##     lb                0.132    0.042    3.136    0.002    0.132    0.131
##     ls                0.711    0.044   16.154    0.000    0.711    0.705
##     la                0.113    0.040    2.842    0.004    0.113    0.108
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 15 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        27
##                                                       
##                                                   Used       Total
##   Number of observations                           255         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 6.773
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.080
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   din ~                                                                 
##     din_1    (ns1)    0.181    0.054    3.368    0.001    0.181    0.183
##     chla     (nt1)   -0.165    0.051   -3.227    0.001   -0.165   -0.167
##     hzoop_1  (nn1)    0.024    0.053    0.456    0.648    0.024    0.024
##     pzoop_1  (nn2)    0.214    0.055    3.871    0.000    0.214    0.212
##     corbic_1 (nn3)   -0.053    0.053   -0.996    0.319   -0.053   -0.052
##     flow     (na1)   -0.400    0.059   -6.764    0.000   -0.400   -0.370
##     temp     (na2)    0.052    0.053    0.984    0.325    0.052    0.052
##     turbid   (na3)    0.053    0.051    1.045    0.296    0.053    0.054
##   chla ~                                                                
##     din_1    (cb1)    0.034    0.064    0.528    0.598    0.034    0.034
##     chla_1   (cs1)    0.280    0.060    4.647    0.000    0.280    0.282
##     hzoop_1  (ct1)   -0.059    0.062   -0.947    0.344   -0.059   -0.058
##     corbic_1 (ct2)    0.015    0.062    0.237    0.813    0.015    0.014
##     pzoop_1  (ct3)   -0.162    0.065   -2.493    0.013   -0.162   -0.159
##     flow     (ca1)   -0.030    0.069   -0.437    0.662   -0.030   -0.028
##     temp     (ca2)    0.140    0.061    2.293    0.022    0.140    0.140
##     turbid   (ca3)    0.075    0.059    1.273    0.203    0.075    0.077
##   corbic ~                                                              
##     chla_1   (lb1)    0.002    0.053    0.038    0.970    0.002    0.002
##     hzoop_1  (lb2)    0.035    0.056    0.621    0.535    0.035    0.035
##     pzoop_1  (lb3)   -0.034    0.057   -0.584    0.559   -0.034   -0.033
##     corbic_1 (ls1)    0.464    0.056    8.282    0.000    0.464    0.462
##     flow     (la1)    0.016    0.061    0.264    0.792    0.016    0.015
##     temp     (la2)   -0.046    0.055   -0.836    0.403   -0.046   -0.047
##     turbid   (la3)   -0.111    0.053   -2.086    0.037   -0.111   -0.115
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .din ~~                                                                
##    .corbic           -0.041    0.043   -0.959    0.337   -0.041   -0.060
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .din               0.644    0.057   11.292    0.000    0.644    0.654
##    .chla              0.883    0.078   11.292    0.000    0.883    0.881
##    .corbic            0.722    0.064   11.292    0.000    0.722    0.746
## 
## R-Square:
##                    Estimate
##     din               0.346
##     chla              0.119
##     corbic            0.254
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     ns                0.181    0.054    3.368    0.001    0.181    0.183
##     nt                0.165    0.051    3.227    0.001    0.165    0.167
##     nn                0.222    0.053    4.199    0.000    0.222    0.220
##     na                0.407    0.059    6.871    0.000    0.407    0.378
##     cb                0.034    0.064    0.528    0.598    0.034    0.034
##     cs                0.280    0.060    4.647    0.000    0.280    0.282
##     ct                0.173    0.060    2.881    0.004    0.173    0.170
##     ca                0.162    0.061    2.649    0.008    0.162    0.162
##     lb                0.048    0.062    0.785    0.433    0.048    0.048
##     ls                0.464    0.056    8.282    0.000    0.464    0.462
##     la                0.121    0.054    2.229    0.026    0.121    0.125
summary(modfitS, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 14 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        26
##                                                       
##                                                   Used       Total
##   Number of observations                           256         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 2.413
##   Degrees of freedom                                 4
##   P-value (Chi-square)                           0.660
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   din ~                                                                 
##     din_1    (ns1)    0.195    0.061    3.178    0.001    0.195    0.195
##     chla     (nt1)    0.014    0.059    0.235    0.814    0.014    0.014
##     hzoop_1  (nn1)   -0.011    0.063   -0.170    0.865   -0.011   -0.010
##     pzoop_1  (nn2)    0.021    0.064    0.333    0.739    0.021    0.020
##     corbic_1 (nn3)    0.074    0.059    1.237    0.216    0.074    0.073
##     flow     (na1)   -0.084    0.064   -1.321    0.187   -0.084   -0.079
##     temp     (na2)    0.089    0.060    1.496    0.135    0.089    0.088
##     turbid   (na3)    0.264    0.063    4.183    0.000    0.264    0.262
##   chla ~                                                                
##     din_1    (cb1)   -0.015    0.062   -0.250    0.802   -0.015   -0.016
##     chla_1   (cs1)    0.285    0.060    4.716    0.000    0.285    0.284
##     hzoop_1  (ct1)    0.073    0.064    1.147    0.252    0.073    0.070
##     corbic_1 (ct2)    0.078    0.060    1.299    0.194    0.078    0.078
##     flow     (ca1)   -0.148    0.063   -2.336    0.019   -0.148   -0.141
##     temp     (ca2)    0.009    0.059    0.146    0.884    0.009    0.009
##     turbid   (ca3)    0.027    0.064    0.430    0.667    0.027    0.028
##   corbic ~                                                              
##     chla_1   (lb1)    0.052    0.059    0.874    0.382    0.052    0.051
##     hzoop_1  (lb2)   -0.026    0.061   -0.423    0.672   -0.026   -0.025
##     pzoop_1  (lb3)   -0.042    0.063   -0.662    0.508   -0.042   -0.039
##     corbic_1 (ls1)    0.322    0.057    5.603    0.000    0.322    0.325
##     flow     (la1)    0.073    0.061    1.197    0.231    0.073    0.069
##     temp     (la2)   -0.072    0.058   -1.245    0.213   -0.072   -0.072
##     turbid   (la3)    0.182    0.058    3.133    0.002    0.182    0.183
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .din ~~                                                                
##    .corbic           -0.070    0.053   -1.335    0.182   -0.070   -0.084
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .din               0.864    0.076   11.314    0.000    0.864    0.839
##    .chla              0.874    0.077   11.314    0.000    0.874    0.875
##    .corbic            0.813    0.072   11.314    0.000    0.813    0.810
## 
## R-Square:
##                    Estimate
##     din               0.161
##     chla              0.125
##     corbic            0.190
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     ns                0.195    0.061    3.178    0.001    0.195    0.195
##     nt                0.014    0.059    0.235    0.814    0.014    0.014
##     nn                0.077    0.061    1.262    0.207    0.077    0.077
##     na                0.291    0.066    4.433    0.000    0.291    0.287
##     cb                0.015    0.062    0.250    0.802    0.015    0.016
##     cs                0.285    0.060    4.716    0.000    0.285    0.284
##     ct                0.107    0.058    1.824    0.068    0.107    0.105
##     ca                0.151    0.065    2.328    0.020    0.151    0.143
##     lb                0.071    0.066    1.081    0.280    0.071    0.069
##     ls                0.322    0.057    5.603    0.000    0.322    0.325
##     la                0.209    0.055    3.834    0.000    0.209    0.208
#modificationindices(modfitW, sort=T, maximum.number=20)
#residuals(modfitW)

labelsfarwest=createLabels(modfitFW, cnameslag)
labelswest=createLabels(modfitW, cnameslag)
labelsnorth=createLabels(modfitN, cnameslag)
labelssouth=createLabels(modfitS, cnameslag)

#FAR WEST
myLavaanPlot(model=modfitFW, labels=labelsfarwest,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#WEST
myLavaanPlot(model=modfitW, labels=labelswest,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#NORTH
myLavaanPlot(model=modfitN, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#SOUTH
myLavaanPlot(model=modfitS, labels=labelssouth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#FAR WEST
generateInputDf(fit=modfitFW, reference_df=cnameslag) %>%
  getLowerTrophicCoord() %>%
  createGraph(fit=modfitFW) %>%
  DiagrammeR::render_graph(title="Far West")
#WEST
generateInputDf(fit=modfitW, reference_df=cnameslag) %>%
  getLowerTrophicCoord() %>%
  createGraph(fit=modfitW) %>%
  DiagrammeR::render_graph(title="West")
#NORTH
generateInputDf(fit=modfitN, reference_df=cnameslag) %>%
  getLowerTrophicCoord() %>%
  createGraph(fit=modfitN) %>%
  DiagrammeR::render_graph(title="North")
#SOUTH
generateInputDf(fit=modfitS, reference_df=cnameslag) %>%
  getLowerTrophicCoord() %>%
  createGraph(fit=modfitS) %>%
  DiagrammeR::render_graph(title="South")

Total effects

ssFW=standardizedsolution(modfitFW) %>% mutate(region="Far West")
ssW=standardizedsolution(modfitW) %>% mutate(region="West")
ssN=standardizedsolution(modfitN) %>% mutate(region="North")
ssS=standardizedsolution(modfitS) %>% mutate(region="South")

sslt=rbind(ssFW,ssW,ssN,ssS) %>% filter(op==":=") %>% select(region,lhs,est.std:ci.upper) %>% 
  separate(lhs,c("variable","influence"), sep=1) %>% 
  mutate(variable=case_when(variable=="n" ~ "DIN",
                            variable=="c" ~ "phytoplankton",
                            variable=="l" ~ "clams"),
         influence=case_when(influence=="b" ~ "bottom-up",
                            influence=="t" ~ "top-down",
                            influence=="s" ~ "self-regulation",
                            influence=="a" ~ "abiotic drivers",
                            influence=="n" ~ "nutrient cycling"),
         region=factor(region, levels=regionorder),
         influence=factor(influence, levels=c("self-regulation","bottom-up","top-down","abiotic drivers","nutrient cycling")),
         variable=factor(variable,levels=c("clams","phytoplankton","DIN")),
         sig=ifelse(pvalue<0.05,"*",""))

ggplot(sslt,aes(x=influence,y=est.std)) +
  facet_grid(variable~region) +
  geom_errorbar(aes(ymin=ci.lower, ymax=ci.upper),width=0.5) +
  geom_point() +
  geom_text(aes(y=ci.upper+0.05, label=sig)) +
  geom_hline(yintercept = 0) +
  theme_bw() + theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1)) +
  labs(y="total effect (standardized)")

#ggsave("../lteffects.png",width = 6,height=5)

Zooplankton-centered model (individual groups)

modFW='chla~chla_1+hcope_1+amphi_1+potam_1+flow+turbid+temp
       hcope~chla_1+hcope_1+pcope_1+potam_1+flow+turbid+temp+estfish_bsmt_1
       amphi~chla_1+amphi_1+flow+turbid+temp+estfish_bsmt_1
       pcope~hcope_1+pcope_1+potam_1+flow+turbid+temp+estfish_bsmt_1
'
modW='chla~chla_1+hcope_1+amphi_1+potam_1+flow+turbid+temp+mysid_1
       hcope~chla_1+hcope_1+pcope_1+mysid_1+potam_1+flow+turbid+temp+estfish_bsmt_1
       amphi~chla_1+amphi_1+mysid_1+flow+turbid+temp+estfish_bsmt_1
       pcope~hcope_1+pcope_1+mysid_1+potam_1+flow+turbid+temp+estfish_bsmt_1
       mysid~chla_1+hcope_1+pcope_1+amphi_1+mysid_1+flow+turbid+temp+estfish_bsmt_1
'
modN='chla~chla_1+hcope_1+amphi_1+corbic_1+flow+turbid+temp
       hcope~chla_1+hcope_1+pcope_1+mysid_1+corbic_1+flow+turbid+temp+estfish_bsmt_1
       amphi~chla_1+amphi_1+flow+turbid+temp+estfish_bsmt_1
       pcope~hcope_1+pcope_1+mysid_1+corbic_1+flow+turbid+temp+estfish_bsmt_1
       mysid~hcope_1+pcope_1+mysid_1+amphi_1+flow+turbid+temp+estfish_bsmt_1
'
modS='chla~chla_1+hcope_1+clad_1+corbic_1+flow+turbid+temp
       hcope~chla_1+hcope_1+pcope_1+corbic_1+flow+turbid+temp+estfish_bsmt_1
       clad~chla_1+clad_1+pcope_1+flow+turbid+temp+estfish_bsmt_1
       amphi~chla_1+amphi_1+flow+turbid+temp+estfish_bsmt_1
       pcope~chla_1+hcope_1+clad_1+pcope_1+corbic_1+flow+turbid+temp+estfish_bsmt_1
'
modfitFW=sem(modFW, data=filter(fdr_ds,region=="Far West"))
modfitW=sem(modW, data=filter(fdr_ds,region=="West"))
modfitN=sem(modN, data=filter(fdr_ds,region=="North"))
modfitS=sem(modS, data=filter(fdr_ds,region=="South"))
summary(modfitFW, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 35 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        38
##                                                       
##                                                   Used       Total
##   Number of observations                           203         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                 5.383
##   Degrees of freedom                                 8
##   P-value (Chi-square)                           0.716
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.230    0.067    3.411    0.001    0.230    0.231
##     hcope_1           0.068    0.055    1.256    0.209    0.068    0.086
##     amphi_1           0.020    0.059    0.336    0.737    0.020    0.025
##     potam_1          -0.002    0.050   -0.044    0.965   -0.002   -0.003
##     flow              0.012    0.070    0.174    0.861    0.012    0.014
##     turbid           -0.028    0.067   -0.412    0.680   -0.028   -0.032
##     temp              0.107    0.059    1.803    0.071    0.107    0.127
##   hcope ~                                                               
##     chla_1            0.107    0.081    1.326    0.185    0.107    0.085
##     hcope_1           0.239    0.068    3.487    0.000    0.239    0.239
##     pcope_1           0.075    0.069    1.083    0.279    0.075    0.074
##     potam_1          -0.146    0.062   -2.351    0.019   -0.146   -0.157
##     flow             -0.069    0.082   -0.843    0.399   -0.069   -0.062
##     turbid           -0.032    0.081   -0.391    0.696   -0.032   -0.029
##     temp             -0.060    0.073   -0.819    0.413   -0.060   -0.057
##     estfish_bsmt_1   -0.130    0.073   -1.783    0.075   -0.130   -0.127
##   amphi ~                                                               
##     chla_1            0.030    0.039    0.774    0.439    0.030    0.024
##     amphi_1           0.790    0.034   23.082    0.000    0.790    0.798
##     flow             -0.248    0.040   -6.202    0.000   -0.248   -0.221
##     turbid            0.010    0.039    0.247    0.805    0.010    0.009
##     temp              0.009    0.034    0.258    0.796    0.009    0.008
##     estfish_bsmt_1    0.025    0.033    0.743    0.458    0.025    0.024
##   pcope ~                                                               
##     hcope_1           0.029    0.065    0.452    0.651    0.029    0.030
##     pcope_1           0.278    0.066    4.231    0.000    0.278    0.281
##     potam_1          -0.089    0.059   -1.497    0.134   -0.089   -0.098
##     flow              0.253    0.078    3.233    0.001    0.253    0.231
##     turbid            0.057    0.077    0.736    0.462    0.057    0.053
##     temp              0.155    0.070    2.220    0.026    0.155    0.150
##     estfish_bsmt_1    0.003    0.069    0.040    0.968    0.003    0.003
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.032    0.052    0.613    0.540    0.032    0.043
##    .amphi             0.004    0.024    0.183    0.855    0.004    0.013
##    .pcope             0.004    0.049    0.089    0.929    0.004    0.006
##  .hcope ~~                                                              
##    .amphi            -0.004    0.030   -0.133    0.894   -0.004   -0.009
##    .pcope            -0.166    0.061   -2.723    0.006   -0.166   -0.195
##  .amphi ~~                                                              
##    .pcope             0.017    0.028    0.604    0.546    0.017    0.042
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.606    0.060   10.075    0.000    0.606    0.910
##    .hcope             0.897    0.089   10.075    0.000    0.897    0.850
##    .amphi             0.199    0.020   10.075    0.000    0.199    0.190
##    .pcope             0.815    0.081   10.075    0.000    0.815    0.809
## 
## R-Square:
##                    Estimate
##     chla              0.090
##     hcope             0.150
##     amphi             0.810
##     pcope             0.191
summary(modfitW, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 35 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        56
##                                                       
##                                                   Used       Total
##   Number of observations                           215         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                12.718
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           0.176
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.174    0.066    2.622    0.009    0.174    0.177
##     hcope_1           0.045    0.065    0.684    0.494    0.045    0.049
##     amphi_1           0.165    0.061    2.689    0.007    0.165    0.191
##     potam_1          -0.072    0.065   -1.110    0.267   -0.072   -0.081
##     flow              0.131    0.068    1.929    0.054    0.131    0.130
##     turbid            0.055    0.067    0.818    0.414    0.055    0.060
##     temp             -0.100    0.059   -1.698    0.090   -0.100   -0.112
##     mysid_1          -0.131    0.065   -2.014    0.044   -0.131   -0.144
##   hcope ~                                                               
##     chla_1            0.095    0.067    1.418    0.156    0.095    0.089
##     hcope_1           0.276    0.068    4.042    0.000    0.276    0.277
##     pcope_1          -0.046    0.058   -0.800    0.424   -0.046   -0.050
##     mysid_1           0.037    0.070    0.532    0.594    0.037    0.038
##     potam_1          -0.194    0.064   -3.024    0.002   -0.194   -0.201
##     flow             -0.227    0.072   -3.155    0.002   -0.227   -0.207
##     turbid           -0.127    0.070   -1.811    0.070   -0.127   -0.127
##     temp              0.046    0.062    0.737    0.461    0.046    0.047
##     estfish_bsmt_1    0.028    0.059    0.475    0.634    0.028    0.030
##   amphi ~                                                               
##     chla_1           -0.063    0.039   -1.608    0.108   -0.063   -0.056
##     amphi_1           0.794    0.037   21.276    0.000    0.794    0.800
##     mysid_1           0.098    0.038    2.604    0.009    0.098    0.094
##     flow              0.019    0.041    0.472    0.637    0.019    0.017
##     turbid           -0.122    0.040   -3.081    0.002   -0.122   -0.115
##     temp             -0.105    0.036   -2.935    0.003   -0.105   -0.102
##     estfish_bsmt_1   -0.165    0.036   -4.582    0.000   -0.165   -0.164
##   pcope ~                                                               
##     hcope_1          -0.138    0.061   -2.240    0.025   -0.138   -0.132
##     pcope_1           0.442    0.055    8.105    0.000    0.442    0.453
##     mysid_1           0.129    0.064    2.002    0.045    0.129    0.124
##     potam_1           0.012    0.060    0.196    0.844    0.012    0.012
##     flow              0.193    0.066    2.942    0.003    0.193    0.169
##     turbid           -0.167    0.064   -2.584    0.010   -0.167   -0.159
##     temp              0.213    0.056    3.802    0.000    0.213    0.210
##     estfish_bsmt_1    0.002    0.056    0.038    0.970    0.002    0.002
##   mysid ~                                                               
##     chla_1            0.179    0.060    2.956    0.003    0.179    0.166
##     hcope_1           0.052    0.059    0.883    0.377    0.052    0.052
##     pcope_1           0.122    0.052    2.335    0.020    0.122    0.130
##     amphi_1          -0.127    0.056   -2.267    0.023   -0.127   -0.134
##     mysid_1           0.372    0.061    6.049    0.000    0.372    0.372
##     flow             -0.254    0.063   -4.049    0.000   -0.254   -0.230
##     turbid            0.263    0.062    4.258    0.000    0.263    0.261
##     temp              0.093    0.054    1.703    0.089    0.093    0.095
##     estfish_bsmt_1   -0.025    0.054   -0.461    0.645   -0.025   -0.026
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.172    0.050    3.445    0.001    0.172    0.242
##    .amphi            -0.006    0.028   -0.225    0.822   -0.006   -0.015
##    .pcope             0.015    0.045    0.336    0.737    0.015    0.023
##    .mysid             0.085    0.043    1.966    0.049    0.085    0.135
##  .hcope ~~                                                              
##    .amphi            -0.029    0.029   -1.000    0.317   -0.029   -0.068
##    .pcope             0.117    0.047    2.472    0.013    0.117    0.171
##    .mysid             0.170    0.046    3.695    0.000    0.170    0.260
##  .amphi ~~                                                              
##    .pcope             0.053    0.027    1.941    0.052    0.053    0.134
##    .mysid            -0.029    0.026   -1.101    0.271   -0.029   -0.075
##  .pcope ~~                                                              
##    .mysid             0.060    0.041    1.458    0.145    0.060    0.100
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.684    0.066   10.368    0.000    0.684    0.853
##    .hcope             0.743    0.072   10.368    0.000    0.743    0.788
##    .amphi             0.251    0.024   10.368    0.000    0.251    0.236
##    .pcope             0.629    0.061   10.368    0.000    0.629    0.609
##    .mysid             0.575    0.055   10.368    0.000    0.575    0.601
## 
## R-Square:
##                    Estimate
##     chla              0.147
##     hcope             0.212
##     amphi             0.764
##     pcope             0.391
##     mysid             0.399
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 33 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        53
##                                                       
##                                                   Used       Total
##   Number of observations                           207         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                17.901
##   Degrees of freedom                                12
##   P-value (Chi-square)                           0.119
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.206    0.073    2.840    0.005    0.206    0.190
##     hcope_1          -0.074    0.090   -0.821    0.411   -0.074   -0.060
##     amphi_1           0.077    0.072    1.066    0.286    0.077    0.073
##     corbic_1          0.002    0.071    0.025    0.980    0.002    0.002
##     flow              0.081    0.079    1.032    0.302    0.081    0.074
##     turbid            0.080    0.071    1.121    0.262    0.080    0.079
##     temp              0.146    0.069    2.108    0.035    0.146    0.148
##   hcope ~                                                               
##     chla_1           -0.021    0.044   -0.481    0.631   -0.021   -0.025
##     hcope_1           0.157    0.072    2.175    0.030    0.157    0.164
##     pcope_1          -0.085    0.046   -1.861    0.063   -0.085   -0.116
##     mysid_1           0.078    0.063    1.226    0.220    0.078    0.097
##     corbic_1          0.087    0.043    2.019    0.044    0.087    0.107
##     flow             -0.317    0.054   -5.873    0.000   -0.317   -0.370
##     turbid            0.068    0.050    1.362    0.173    0.068    0.086
##     temp              0.127    0.045    2.799    0.005    0.127    0.166
##     estfish_bsmt_1    0.026    0.047    0.555    0.579    0.026    0.035
##   amphi ~                                                               
##     chla_1            0.107    0.058    1.863    0.062    0.107    0.107
##     amphi_1           0.533    0.057    9.356    0.000    0.533    0.542
##     flow             -0.006    0.064   -0.097    0.923   -0.006   -0.006
##     turbid           -0.014    0.056   -0.246    0.806   -0.014   -0.015
##     temp             -0.054    0.054   -0.997    0.319   -0.054   -0.059
##     estfish_bsmt_1   -0.081    0.055   -1.491    0.136   -0.081   -0.093
##   pcope ~                                                               
##     hcope_1          -0.074    0.106   -0.700    0.484   -0.074   -0.058
##     pcope_1           0.259    0.067    3.889    0.000    0.259    0.262
##     mysid_1           0.137    0.092    1.489    0.137    0.137    0.128
##     corbic_1          0.041    0.067    0.616    0.538    0.041    0.038
##     flow             -0.063    0.080   -0.794    0.427   -0.063   -0.055
##     turbid           -0.264    0.073   -3.592    0.000   -0.264   -0.252
##     temp              0.079    0.067    1.178    0.239    0.079    0.077
##     estfish_bsmt_1   -0.030    0.069   -0.440    0.660   -0.030   -0.031
##   mysid ~                                                               
##     hcope_1           0.018    0.088    0.203    0.839    0.018    0.015
##     pcope_1          -0.051    0.056   -0.927    0.354   -0.051   -0.057
##     mysid_1           0.208    0.076    2.738    0.006    0.208    0.210
##     amphi_1          -0.100    0.052   -1.944    0.052   -0.100   -0.099
##     flow             -0.360    0.066   -5.463    0.000   -0.360   -0.340
##     turbid            0.213    0.061    3.461    0.001    0.213    0.220
##     temp              0.112    0.055    2.025    0.043    0.112    0.118
##     estfish_bsmt_1    0.065    0.057    1.144    0.253    0.065    0.072
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.048    0.042    1.127    0.260    0.048    0.079
##    .amphi             0.003    0.052    0.055    0.956    0.003    0.004
##    .pcope            -0.062    0.063   -0.994    0.320   -0.062   -0.069
##    .mysid             0.107    0.053    2.040    0.041    0.107    0.143
##  .hcope ~~                                                              
##    .amphi             0.015    0.034    0.452    0.651    0.015    0.031
##    .pcope             0.111    0.042    2.645    0.008    0.111    0.187
##    .mysid             0.218    0.037    5.830    0.000    0.218    0.443
##  .amphi ~~                                                              
##    .pcope            -0.119    0.051   -2.343    0.019   -0.119   -0.165
##    .mysid             0.045    0.042    1.069    0.285    0.045    0.075
##  .pcope ~~                                                              
##    .mysid             0.193    0.052    3.687    0.000    0.193    0.265
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.926    0.091   10.173    0.000    0.926    0.922
##    .hcope             0.401    0.039   10.173    0.000    0.401    0.657
##    .amphi             0.596    0.059   10.173    0.000    0.596    0.686
##    .pcope             0.878    0.086   10.173    0.000    0.878    0.804
##    .mysid             0.605    0.059   10.173    0.000    0.605    0.653
## 
## R-Square:
##                    Estimate
##     chla              0.078
##     hcope             0.343
##     amphi             0.314
##     pcope             0.196
##     mysid             0.347
summary(modfitS, standardized=T, rsq=T)
## lavaan 0.6-9 ended normally after 24 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        52
##                                                       
##                                                   Used       Total
##   Number of observations                           210         312
##                                                                   
## Model Test User Model:
##                                                       
##   Test statistic                                19.526
##   Degrees of freedom                                13
##   P-value (Chi-square)                           0.108
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.220    0.070    3.149    0.002    0.220    0.213
##     hcope_1           0.090    0.071    1.271    0.204    0.090    0.085
##     clad_1            0.185    0.067    2.759    0.006    0.185    0.188
##     corbic_1         -0.015    0.065   -0.237    0.813   -0.015   -0.015
##     flow             -0.138    0.080   -1.717    0.086   -0.138   -0.121
##     turbid            0.013    0.066    0.193    0.847    0.013    0.013
##     temp              0.060    0.065    0.913    0.361    0.060    0.059
##   hcope ~                                                               
##     chla_1            0.164    0.056    2.943    0.003    0.164    0.168
##     hcope_1           0.291    0.061    4.757    0.000    0.291    0.290
##     pcope_1           0.002    0.053    0.029    0.977    0.002    0.002
##     corbic_1          0.110    0.055    1.980    0.048    0.110    0.113
##     flow             -0.296    0.066   -4.500    0.000   -0.296   -0.274
##     turbid           -0.076    0.056   -1.359    0.174   -0.076   -0.080
##     temp              0.154    0.056    2.757    0.006    0.154    0.161
##     estfish_bsmt_1   -0.182    0.056   -3.262    0.001   -0.182   -0.190
##   clad ~                                                                
##     chla_1            0.171    0.061    2.810    0.005    0.171    0.162
##     clad_1            0.513    0.059    8.653    0.000    0.513    0.511
##     pcope_1          -0.023    0.055   -0.416    0.677   -0.023   -0.023
##     flow              0.125    0.066    1.889    0.059    0.125    0.107
##     turbid           -0.014    0.059   -0.242    0.809   -0.014   -0.014
##     temp              0.135    0.059    2.303    0.021    0.135    0.130
##     estfish_bsmt_1   -0.125    0.057   -2.206    0.027   -0.125   -0.121
##   amphi ~                                                               
##     chla_1           -0.026    0.070   -0.371    0.710   -0.026   -0.025
##     amphi_1           0.208    0.067    3.094    0.002    0.208    0.205
##     flow              0.066    0.079    0.830    0.407    0.066    0.055
##     turbid            0.158    0.071    2.222    0.026    0.158    0.152
##     temp              0.083    0.070    1.171    0.242    0.083    0.079
##     estfish_bsmt_1    0.055    0.071    0.778    0.436    0.055    0.053
##   pcope ~                                                               
##     chla_1            0.243    0.065    3.729    0.000    0.243    0.232
##     hcope_1          -0.058    0.069   -0.837    0.403   -0.058   -0.053
##     clad_1            0.052    0.064    0.809    0.419    0.052    0.052
##     pcope_1           0.446    0.062    7.189    0.000    0.446    0.448
##     corbic_1         -0.043    0.062   -0.697    0.486   -0.043   -0.041
##     flow              0.046    0.075    0.616    0.538    0.046    0.040
##     turbid           -0.089    0.063   -1.414    0.157   -0.089   -0.087
##     temp              0.017    0.063    0.269    0.788    0.017    0.016
##     estfish_bsmt_1   -0.056    0.063   -0.887    0.375   -0.056   -0.054
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.076    0.050    1.518    0.129    0.076    0.105
##    .clad              0.206    0.055    3.773    0.000    0.206    0.270
##    .amphi             0.028    0.064    0.437    0.662    0.028    0.030
##    .pcope            -0.017    0.056   -0.300    0.764   -0.017   -0.021
##  .hcope ~~                                                              
##    .clad              0.071    0.044    1.610    0.107    0.071    0.112
##    .amphi             0.042    0.053    0.783    0.434    0.042    0.054
##    .pcope             0.032    0.046    0.696    0.487    0.032    0.048
##  .clad ~~                                                               
##    .amphi            -0.103    0.056   -1.831    0.067   -0.103   -0.127
##    .pcope             0.094    0.050    1.897    0.058    0.094    0.132
##  .amphi ~~                                                              
##    .pcope             0.025    0.059    0.413    0.679    0.025    0.029
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.872    0.085   10.247    0.000    0.872    0.857
##    .hcope             0.600    0.059   10.247    0.000    0.600    0.660
##    .clad              0.671    0.065   10.247    0.000    0.671    0.628
##    .amphi             0.983    0.096   10.247    0.000    0.983    0.908
##    .pcope             0.755    0.074   10.247    0.000    0.755    0.713
## 
## R-Square:
##                    Estimate
##     chla              0.143
##     hcope             0.340
##     clad              0.372
##     amphi             0.092
##     pcope             0.287
#modificationindices(modfitS, sort=T, maximum.number=20)
#residuals(modfitW)

labelsfarwest=createLabels(modfitFW, cnameslag)
labelswest=createLabels(modfitW, cnameslag)
labelsnorth=createLabels(modfitN, cnameslag)
labelssouth=createLabels(modfitS, cnameslag)

#FAR WEST
myLavaanPlot(model=modfitFW, labels=labelsfarwest,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#WEST
myLavaanPlot(model=modfitW, labels=labelswest,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#NORTH
myLavaanPlot(model=modfitN, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#SOUTH
myLavaanPlot(model=modfitS, labels=labelssouth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#FAR WEST
generateInputDf(fit=modfitFW, reference_df=cnameslag) %>%
  getZoopCoord(region="Far West") %>%
  createGraph(fit=modfitFW) %>%
  DiagrammeR::render_graph(title="Far West")
#WEST
generateInputDf(fit=modfitW, reference_df=cnameslag) %>%
  getZoopCoord(region="West") %>%
  createGraph(fit=modfitW) %>%
  DiagrammeR::render_graph(title="West")
#NORTH
generateInputDf(fit=modfitN, reference_df=cnameslag) %>%
  getZoopCoord(region="North") %>%
  createGraph(fit=modfitN) %>%
  DiagrammeR::render_graph(title="North")
#SOUTH
generateInputDf(fit=modfitS, reference_df=cnameslag) %>%
  getZoopCoord(region="South") %>%
  createGraph(fit=modfitS) %>%
  DiagrammeR::render_graph(title="South")

Total effects

Haven’t done yet.